package cn.com.dycc.party_org.membership.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.dycc.party_org.member.dao.MemberDao;
import cn.com.dycc.party_org.membership.dao.MemberShipDao;
import cn.com.dycc.party_org.membership.entity.MemberShipEntity;
import cn.com.dycc.party_org.membership.param.AddMemberShipParam;
import cn.com.dycc.sys_manage.usersysmanagement.dao.UserDao;
import cn.com.dycc.util.DateUtil;
import cn.com.dycc.util.IntegerUtil;

@Service("memberShipService")
@Transactional(rollbackFor=Exception.class)
public class MemberShipServiceImpl implements MemberShipService{
	
	@Resource(name="memberShipDao")
	private MemberShipDao memberShipDao;
	
	@Resource(name="userDao")
	private UserDao userDao;
	
	@Resource(name="memberDao")
	private MemberDao memberDao;
	
	//增加党籍信息 HttpServletRequest request,
	public String addMemberShip(HttpServletRequest request,AddMemberShipParam addMemberShipParam) throws NotFoundMemberShipException {
		
		if(addMemberShipParam.getUser_id()==null||addMemberShipParam.getUser_id().trim().isEmpty()){
			throw new NotFoundMemberShipException("请选择需要变更信息的党员！");
		}
		
		if(!(IntegerUtil.isNumeric(addMemberShipParam.getUser_id()))){
			throw new NotFoundMemberShipException("变更党籍信息的党员id为整型！");
		}
		
		if(addMemberShipParam.getParty_type()==null||addMemberShipParam.getParty_type().trim().isEmpty()){
			throw new NotFoundMemberShipException("请选择党籍类别！");
		}
		
		if(!(IntegerUtil.isNumeric(addMemberShipParam.getParty_type()))){
			throw new NotFoundMemberShipException("党籍类别为整型！");
		}
		
		if(addMemberShipParam.getParty_status()==null||addMemberShipParam.getParty_status().trim().isEmpty()){
			throw new NotFoundMemberShipException("请选择党籍状态！");
		}
		
		if(!(IntegerUtil.isNumeric(addMemberShipParam.getParty_status()))){
			throw new NotFoundMemberShipException("党籍状态为整型！");
		}
		
//		String format = "yyyy-MM-dd";
		//date转字符串
//		String datetime = DateUtil.format_DateToString(addMemberShipParam.getMembership_date(), format);
		
		if(addMemberShipParam.getMembership_string()==null||addMemberShipParam.getMembership_string().trim().isEmpty()){
			throw new NotFoundMemberShipException("请党籍变更时间！");
		}
		
		if(addMemberShipParam.getMembership_desc()==null||addMemberShipParam.getMembership_desc().trim().isEmpty()){
			throw new NotFoundMemberShipException("请输入党籍变更说明！");
		}
		
		String org_id = request.getSession().getAttribute("org_id").toString();
//		String org_id = "1";
		
		String opt_user_id = request.getSession().getAttribute("id").toString();
//		String opt_user_id = "1";
		//获取用户名称
		String user_name = userDao.findUserNameByUserId(addMemberShipParam.getUser_id(),org_id);
		
		int count = memberShipDao.findCountByUserIdAndPartyType(addMemberShipParam.getUser_id(),addMemberShipParam.getParty_type(),org_id);
		
		if(count>0){
			throw new NotFoundMemberShipException("该用户的该阶段类别的党籍信息已存在！");
		}
		
		//获取该党员的所在党支部信息
		Map<String,Object> map = memberDao.findDeptPartyByUserId(org_id,addMemberShipParam.getUser_id());
		String dept_id = null;
		String dept_name = null;
		if(map!=null){
			dept_name = map.get("dept_name").toString();
			dept_id = map.get("dept_id").toString();
		}	
		
		//String 类型时间转Date类型
		String format = "yyyy-MM-dd";
		Date date = DateUtil.format_StringToDate(addMemberShipParam.getMembership_string(), format);
//		System.out.println(date);
		
		addMemberShipParam.setDept_id(dept_id);
		addMemberShipParam.setDept_name(dept_name);
		addMemberShipParam.setMembership_date(date);
		addMemberShipParam.setOpt_user_id(opt_user_id);
		addMemberShipParam.setUser_name(user_name);
		addMemberShipParam.setOrg_id(org_id);
		
		int add = memberShipDao.addMemberShip(addMemberShipParam);
		
		if(add==0){
			throw new NotFoundMemberShipException("增加党员党籍信息失败！");
		}
		
		return "增加党员党籍成功！";
	}
	
	//根据id删除党籍信息
	public String delMemberShipByInfoId(HttpServletRequest request,String info_id) throws NotFoundMemberShipException {
		
		if(info_id==null||info_id.trim().isEmpty()){
			throw new NotFoundMemberShipException("请选择需要删除的人员党籍信息！");
		}
		if(!(IntegerUtil.isNumeric(info_id))){
			throw new NotFoundMemberShipException("删除的党籍信息id为整型！");
		}
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		int del = memberShipDao.delMemberShipByInfoId(info_id,org_id);
		if(del==0){
			throw new NotFoundMemberShipException("删除失败！");
		}
		return "删除成功！";
	}
	
	//查询该党员用户的所有党籍变化信息
	public List<MemberShipEntity> findMemberShipList(HttpServletRequest request, String user_id)
			throws NotFoundMemberShipException {
		
		String org_id = request.getSession().getAttribute("org_id").toString();
		
		if(user_id==null||user_id.trim().isEmpty()){
			throw new NotFoundMemberShipException("请选择需要查询的党员！");
		}
		
		if(!(IntegerUtil.isNumeric(user_id))){
			throw new NotFoundMemberShipException("党员id为整型！");
		}
		
		return memberShipDao.findMemberShipList(user_id, org_id);
	}

}
